package application.exportimport;

import dao.inte.ConfigurationRepository;
import org.springframework.beans.factory.annotation.Autowired;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.xml.bind.JAXBException;
import java.io.FileNotFoundException;
import java.io.IOException;

//TODO az bude applikace na realne DB, tak mozno zaregistrovat export file na zbozi.cz atd

/**
 * Provides function consist of checking dirs and starting generators of servers with goods. (zbozi.cz etc)
 */
@Singleton
public class DatabaseExporter {

    @Inject
    private ConfigurationRepository configurationRepository;
    @Autowired
    private GeneratorZboziCz generatorZboziCz;
    @Autowired
    private GeneratorHeurekaCz generatorHeurekaCz;
    @Autowired
    private GeneratorZboziJyxoCz generatorZboziJyxoCz;
    private String xmlDirectory;

    public DatabaseExporter() {
    }

    public DatabaseExporter(ConfigurationRepository configurationRepository) throws JAXBException, FileNotFoundException {
        this.configurationRepository = configurationRepository;
    }

    public void exportDB() throws PathIsNotSetException, FileNotFoundException, JAXBException, IOException {
        checkDirectory();

        generatorZboziCz.setDirectoryPath(xmlDirectory);
        generatorZboziCz.generate();

        generatorHeurekaCz.setDirectoryPath(xmlDirectory);
        generatorHeurekaCz.generate();

        generatorZboziJyxoCz.setDirectoryPath(xmlDirectory);
        generatorZboziJyxoCz.generate();
    }

    private void checkDirectory() throws PathIsNotSetException {
        xmlDirectory = configurationRepository.getConfig().getXmlDirectory();
        if (xmlDirectory == null) {
            throw new PathIsNotSetException("Xml directory is not set");
        }
    }
}


